<template>
  <!-- 版心  -->
  <el-container class="main-container">
    <!-- 1.左侧菜单栏 -->
    <el-aside width="200px">
      <!-- 1.1用户信息 -->
      <div class="user-box">
        <img v-if="user.user_pic" :src="user.user_pic" alt="" />
        <img v-else :src="user.user_pic" alt="" />
        <span>欢迎{{ user.nickname || user.username }}</span>
      </div>
      <!-- 1.2菜单栏  $router.path是获取当前的hash值,保证刷新还停留在当前页面-->
      <el-menu
        :default-active="$route.path"
        background-color="#21262f"
        text-color="#fff"
        active-text-color="#074ec2"
        unique-opened
        router
      >
        <template v-for="item in menu">
          <!-- 没有子菜单 -->
          <el-menu-item
            :index="item.indexPath"
            :key="item.indexPath"
            v-if="!item.children"
          >
            <i :class="item.icon"></i>
            <span>{{ item.title }}</span>
          </el-menu-item>
          <!-- 有子菜单 -->
          <el-submenu :key="item.indexPath" v-else :index="item.indexPath">
            <template #title>
              <i :class="item.icon"></i>
              <span>{{ item.title }}</span>
            </template>
            <el-menu-item
              v-for="ele in item.children"
              :key="ele.indexPath"
              :index="ele.indexPath"
            >
              <i :class="ele.icon"></i>
              <span>{{ ele.title }}</span>
            </el-menu-item>
          </el-submenu>
        </template>
      </el-menu>
    </el-aside>
    <!-- 2.右侧内容 -->
    <el-container>
      <!-- 2.1 右侧顶部栏 -->
      <el-header>
        <!-- (1)左侧logo -->
        <div class="logo"></div>
        <!-- (2)右侧menu -->
        <el-menu mode="horizontal" router>
          <el-menu-item>
            <img
              class="avatar"
              v-if="user.user_pic"
              :src="user.user_pic"
              alt=""
            />
            <img class="avatar" v-else src="../assets/logo.png" alt="" />
          </el-menu-item>
          <el-submenu index="3">
            <!-- 具名插槽 -->
            <template slot="title">
              <i class="el-icon-user"></i>
              <span>个人中心</span>
            </template>
            <!-- 子菜单 -->
            <el-menu-item index="/user-info"
              ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
            >
            <el-menu-item index="/user-avatar"
              ><i class="el-icon-camera"></i>更换头像</el-menu-item
            >
            <el-menu-item index="/user-pwd"
              ><i class="el-icon-key"></i>重置密码</el-menu-item
            >
          </el-submenu>
          <el-menu-item>
            <i class="el-icon-switch-button"></i>
            <span slot="title" @click="doLogout">退出</span>
          </el-menu-item>
        </el-menu>
      </el-header>
      <!-- 2.2 右侧内容 -->
      <el-main>
        <!-- 二级路由出口 -->
        <router-view></router-view>
      </el-main>
      <!-- 2.3 右侧底部 -->
      <el-footer>© www.itheima.com - 黑马程序员</el-footer>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Main",
  data() {
    return {
      menu: [],
    };
  },
  methods: {
    async doLogout() {
      const res = await this.$confirm("您确定要退出吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).catch(() => {});
      if (res) {
        //清除token,跳转登录页
        this.$store.commit("setToken", "");
        this.$router.push("/login");
      }
    },
  },
  computed: {
    user() {
      return this.$store.state.user;
    },
  },
  //页面一加载,在created中发请求
  async created() {
    //(1)获取用户的信息
    //通知actions更新,因为用户资料的请求要在多个地方使用,所以要在actions中发送
    this.$store.dispatch("getUserInfo");
    //(2)加载左侧菜单
    const { data: res } = await this.$axios.get("/my/menus");
    console.log(res);
    this.menu = res.data;
  },
};
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}

//右侧样式
.el-header {
  .logo {
    height: 45px;
    width: 150px;
    padding: 8px 0px;
    user-select: none;
    text-align: center;
    background: url("@/assets/images/logo.png") center center / 100px no-repeat
      rgb(85, 85, 85);
    border-radius: 3px;
    margin-left: 15px;
  }
}

// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}

// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
</style>
